package com.hmkj.admin.controller.trader;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.common.utils.ValidateUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.trader.Trader;
import com.hmkj.core.service.trader.TraderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class TraderJsonController extends GenericController<Integer, Trader> {
    @Resource
    private TraderService traderService;

    /**
     * 问题反馈
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("trader/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return traderService.getDtGridList(dtGridPager, Trader.class);
    }

    /**
     * 新增
     *
     * @param trader
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("trader/json/add")
    @RequiresPermissions("sys:trader:save")
    public ResultEntity addJson(Trader trader) throws Exception {
        checkModel(trader);
        if (traderService.selectCount(new Trader(t -> {
            t.setName(trader.getName());
        })) > 0) {
            throw new BussinessException("用户名已存在");
        }
        trader.setStatus(Trader.STATUS.T1.code);
        trader.setCreator(AdminSessionHelper.getCurrAdmin().getName());
        traderService.insert(trader);
        return ok();
    }

    /**
     * 编辑
     *
     * @param trader
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("trader/json/edit")
    @RequiresPermissions("sys:trader:edit")
    public ResultEntity editJson(Trader trader) throws Exception {
        checkModel(trader);
        Trader trader1 = checkPrimaryKey(trader.getId());
        if (!trader1.getName().equals(trader.getName())) {
            if (traderService.selectCount(new Trader(t -> {
                t.setName(trader.getName());
            })) > 0) {
                throw new BussinessException("用户名已存在");
            }
        }
        trader.setUpdator(AdminSessionHelper.getCurrAdmin().getName());
        traderService.updateByPrimaryKeySelective(trader);
        return ok();
    }


    /**
     * 发布
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "trader/json/publish")
    @RequiresPermissions("sys:trader:publish")
    public ResultEntity publish(Integer id) throws Exception {
        Trader trader = checkPrimaryKey(id);
        if (trader.getStatus() == Trader.STATUS.T2.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        traderService.updateByPrimaryKeySelective(new Trader(t -> {
            t.setId(trader.getId());
            t.setStatus(Trader.STATUS.T2.code);
            t.setPublishTime(new Date());
        }));
        return ok();
    }

    /**
     * 撤回
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "trader/json/back")
    @RequiresPermissions("sys:trader:back")
    public ResultEntity back(Integer id) throws Exception {
        Trader trader = checkPrimaryKey(id);
        if (trader.getStatus() != Trader.STATUS.T2.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        traderService.updateByPrimaryKeySelective(new Trader(a -> {
            a.setId(trader.getId());
            a.setStatus(Trader.STATUS.T1.code);
        }));
        return ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "trader/json/del")
    @RequiresPermissions("sys:trader:del")
    public ResultEntity del(Integer id) throws Exception {
        Trader trader = checkPrimaryKey(id);
        if (trader.getStatus() == Trader.STATUS.T2.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        traderService.updateByPrimaryKeySelective(new Trader(a -> {
            a.setId(trader.getId());
            a.setDelFlag(Trader.DELFLAG.DELETED.code);
        }));
        return ok();
    }

    private void checkModel(Trader trader) {
        if (StringUtils.isBlank(trader.getName())) {
            throw new BussinessException("用户名不能为空");
        } else if (trader.getName().length() > 30) {
            throw new BussinessException("用户名长度不能超过30位");
        } else if (StringUtils.isBlank(trader.getStockName())) {
            throw new BussinessException("股票名称不能为空");
        } else if (trader.getStockName().length() > 30) {
            throw new BussinessException("股票名称长度不能超过30位");
        } else if (StringUtils.isBlank(trader.getStockNum())) {
            throw new BussinessException("股票数量不能为空");
        } else if (!ValidateUtils.isPrice(trader.getStockNum())) {
            throw new BussinessException("股票数量格式不正确");
        } else if (StringUtils.isBlank(trader.getProfit())) {
            throw new BussinessException("盈利分配不能为空");
        } else if (trader.getProfit().length() > 10) {
            throw new BussinessException("盈利分配长度不能超过10位");
        }
    }

    private Trader checkPrimaryKey(Integer id) throws Exception {
        if (null == id) {
            throw new BussinessException("参数有误");
        } else {
            Trader systemNotice = traderService.selectOne(new Trader(t -> {
                t.setId(id);
                t.setDelFlag(Trader.DELFLAG.NORMAL.code);
            }));
            if (null != systemNotice) {
                return systemNotice;
            } else {
                throw new BussinessException("参数有误");
            }
        }
    }
}
